package com.exigenservices.practice.roadcompanion.server.dao;

import com.exigenservices.practice.roadcompanion.server.entity.User;
import com.exigenservices.practice.roadcompanion.server.entity.UserRoutes;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Repository;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

//TODO Для всех ДАО - транзакция открывать/закрывать не нужно.

/** Транзакция - это набор логически связанных действий в приложении, а не изменение одной записи.
 * В нашем случае они разруливаются на уровне сервиса.
 * entityManager.getTransaction использовать не нужно, вместо вас открывать и закрывать ее должен спринг.
 */
@Repository
public class UserRoutesDAOimpl implements UserRoutesDAO  {
    private static Logger LOGGER = Logger.getLogger(UsersDAOimpl.class.getName());

    @PersistenceContext(unitName = "userroutes")
    private EntityManager entityManager;

    public UserRoutesDAOimpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void createUserRoute(UserRoutes userRoute) throws Exception {

        try{
            entityManager.persist(userRoute);

        } catch (HibernateException e){
            LOGGER.error("UserRoutesDAOimpl::Could not create User Route:: ",e);
            //throw new Exception("Could not create User Route. " + e.getMessage());
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void updateUserRoute(UserRoutes userRoute) throws Exception{
        try {
            UserRoutes route = entityManager.find(UserRoutes.class,userRoute.getID());

            route = userRoute;

        } catch (HibernateException e) {
            LOGGER.error("UserRoutesDAOimpl::Could not update User Route:: ",e);
            //throw new Exception("Could not update User Route. " + e.getMessage());
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void deleteUserRoute(UserRoutes userRoute) throws Exception {
        try {
            entityManager.remove(userRoute);

        } catch (HibernateException e) {
            LOGGER.error("UserRoutesDAOimpl::Could not delete User Route:: ",e);
            //throw new Exception("Could not delete User Route. " + e.getMessage());
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<UserRoutes> getRoute(User user) throws Exception {
        try{
            List<UserRoutes> routes = entityManager.createQuery("select UserRoutes from userrouters where userID = ?" + user.getID()).getResultList();

            return routes;
        } catch (HibernateException e){
            LOGGER.error("UserRoutesDAOimpl::Could not create User Route:: ",e);
            throw new Exception("Could not get route " + e.getMessage());
        }
    }
}
